function zanAndComment(detailId) {
    var id = "#am-comment-main-" + detailId;
    $zan_comment = $(id).find("#zan-comment");
    if ($zan_comment.is(':hidden')) {
        $zan_comment.show();
    }
    else if ($zan_comment.is(':visible')) {
        $zan_comment.hide();
    }
    //alert(zan_comment.attr("hidden"));
}
//点赞
function zan(detailId) {
    var url = "/bbsComment/insertBbsComment";

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: url,
        data: {
            bbsDetailId: detailId,
            type: 1
        },
        success: function (res) {
            if (res.code == "000000") {
                alert("点赞成功");
                var id = "#am-comment-main-" + detailId;
                $zanName = $(id).find("#zanName");
                var user = JSON.parse(localStorage.getItem("user"));
                if ($zanName.attr("value") == "") {
                    $zanName.append(user.nickname);
                } else {
                    $zanName.append("," + user.nickname);
                }
            } else if (res.code == "000011") {
                alert(res.msg);
                localStorage.setItem("user", null);
                window.location.href = "../index.jsp"
            } else if (res.code == "000012") {
                alert(res.msg);
            } else if (res.code == "000013") {
                alert(res.msg);
            }

        },
        error: function (res) {
            alert(res);
        }
    });
    zanAndComment(detailId);
}

function backComment(detailId, toUserId, toUserName) {
    //var id="#am-comment-main-"+detailId;
    $my_comment = $("#my-comment-box");
    if ($my_comment.is(':hidden')) {
        $my_comment.show();
        $my_comment.attr("bbsDetailId", detailId);
        $my_comment.attr("toUserId", toUserId);
        $my_comment.attr("toUserName", toUserName);
    }
    else if ($my_comment.is(':visible')) {
        $my_comment.hide();
        $my_comment.removeAttr("bbsDetailId");
        $my_comment.removeAttr("toUserId");
        $my_comment.removeAttr("toUserName");
    }
}

//打开评论窗口
function openComment(detailId, toUserId, toUserName) {
    //var id="#am-comment-main-"+detailId;
    backComment(detailId, toUserId, toUserName);
    zanAndComment(detailId);
}

//关闭评论窗口
function closeDiv() {
    $("#my-comment-box").hide();
    $("#doc-ta-1").val("");
}

//提交评论
function comment() {
    var bbsDetailId = $("#my-comment-box").attr("bbsDetailId");
    var toUserId = $("#my-comment-box").attr("toUserId");
    var toUserName = $("#my-comment-box").attr("toUserName");
    var content = $("#doc-ta-1").val();
    //alert(content)

    var url = "/bbsComment/insertBbsComment";
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: url,
        data: {
            bbsDetailId: bbsDetailId,
            toUserId: toUserId,
            content: content,
            type: 2
        },
        success: function (res) {
            if (res.code == "000000") {
                alert("评论成功");
                var user = JSON.parse(localStorage.getItem("user"));
                var contentHtml = '';
                if (toUserId == '0') {
                    contentHtml = '<div class="item"><a>' + user.nickname + '</a>:' + content + '</div>'
                } else {
                    var reply = '</a>' + '回复' + '<a >' + toUserName;
                    contentHtml = '<div class="item"><a>' + user.nickname + reply + '</a>:' + content + '</div>'
                }
                var id = "#comment-list-" + bbsDetailId;
                $(id).append(contentHtml);
                closeDiv();
            } else if (res.code == "000011") {
                alert(res.msg);
                localStorage.setItem("user", null);
                window.location.href = "../index.jsp"
            } else if (res.code == "000012") {
                alert(res.msg);
            } else if (res.code == "000013") {
                alert(res.msg);
            }
        },
        error: function (res) {
            alert(res);
        }
    });
}

function addBbsList(categoryCode) {
    var divId = "#am-comment-" + categoryCode;
    var offset = $(divId).attr("offset");
    var rows = $(divId).attr("rows");
    var url = "/bbsDetail/selectBbsDetailList?categoryCode=" + categoryCode + "&offset=" + offset + "&rows=" + rows;
    $.get(url, function (result) {
        var myTemplate = Handlebars.compile($("#bbs-detail-list").html());
        $(divId).append(myTemplate(result));
        //var bbs = "";
        for (var i = 0; i < result.bbsDetailList.length; i++) {
            var bbsDetail = result.bbsDetailList[i];
            var bbsCommentUrl = "/bbsComment/selectBbsCommentByBbsDetailId?bbsDetailId=" + bbsDetail.id;
            $.get(bbsCommentUrl, function (resutl2) {
                var contentHtml = "";
                for (var j = 0; j < resutl2.bbsCommentList.length; j++) {
                    var myTemplate = Handlebars.compile($("#bbs-comment-list").html());
                    var id = "#comment-list-" + resutl2.bbsCommentList[j].bbsDetailId;
                    $(id).html(myTemplate(resutl2));
                }
            })
        }
        if (result.bbsDetailList.length > 0) {
            $(divId).attr("offset", parseInt(offset) + parseInt(rows));
        }
    });

    $("#published").attr("href", "../bbs/published.jsp?categoryCode=" + categoryCode);
}

$(function () {
    $('.am-header-title').text('论坛');

    var user = JSON.parse(localStorage.getItem("user"));
    if (user == null || user == "null") {
        window.location.href = '../../app/login/login.jsp';
    } else {
        if (user.headImage != null) {
            $("#bbsimage").attr("src", user.headImage);
        }
    }


    var categoryCode = 1;
    addBbsList(categoryCode);


    $('body').on('click','.img-box img',function(){
        var prewImg = '<div class="bigImg"><a href="javascript:;">关闭</a><img src="'+ $(this).attr('src') +'" /></div>';
        $('body').append(prewImg);
    });
    $('body').on('click','.bigImg',function(){
        $('.bigImg').remove();
    });










});

function moreBbsList() {
    //var categoryCode=$("#bbsType").find(".am-active").val();
    var categoryCode = 1;
    addBbsList(categoryCode);
}
